﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;

namespace SilverApp.ViewModel
{
    public class ComputerViewModel : ViewModelBase
    {
        private List<LiveService.Folder>                 folders;
        //private ObservableCollection<LiveService.Folder> foldersCollection;
        private ObservableCollection<ComputerItemViewModel> foldersCollection;
        private LiveService.User                         muser;
        private LiveService.Folder selectedfolder;
        HomeViewModel mparent;
        private ICommand doubleCommand;
        private string mtest = "plop";

        public ComputerViewModel()
        {
            ;
        }

        public ComputerViewModel(HomeViewModel parent)
        {
            mparent = parent;
            muser = mparent.CurrentUser;
            doubleCommand = new RelayCommand(NextPage, CanNextPage);
            this.InitUserFolders();
        }

        public HomeViewModel Parent
        {
            get { return this.mparent; }
        }

        public ICommand DoubleCommand
        {
            get { return this.doubleCommand; }
            set { this.doubleCommand = value; }
        }

        private void NextPage(object parameter)
        {
            if (parameter != null)
            {
               // mparent.RegisterCurrentFolder(parameter as ComputerItemViewModel);
                ComputerItemViewModel test = (ComputerItemViewModel) parameter;
               // mparent.CurrentfolderName = test.ModelName;
                mparent.RegisterCurrentComputer(test);
                mparent.RegisterNextView("Folder");
                this.Test = "test";
            }
        }

        private bool CanNextPage(object parameter)
        {
            return true;
        }

        public void InitUserFolders()
        {
            try
            {
                this.Sclient.getUserComputersCompleted += new EventHandler<LiveService.getUserComputersCompletedEventArgs>(InitUserFoldersCompleted);
                this.Sclient.getUserComputersAsync(muser.id);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public void InitUserFoldersCompleted(object sender, LiveService.getUserComputersCompletedEventArgs e)
        {
            this.folders = e.Result;
            /*ObservableCollection<LiveService.Folder> tmpcol = new ObservableCollection<LiveService.Folder>();
            foreach (LiveService.Folder xmp in this.folders)
            {
                this.SelectedFolder = xmp;  
                tmpcol.Add(xmp);
            }*/
            ObservableCollection<ComputerItemViewModel> tmpcol = new ObservableCollection<ComputerItemViewModel>();
            foreach (LiveService.Folder xmp in this.folders)
            {
                tmpcol.Add(new ComputerItemViewModel(xmp, this));
            }
            this.FoldersCollection = tmpcol;
        }

       // public ObservableCollection<LiveService.Folder> FoldersCollection
        public ObservableCollection<ComputerItemViewModel> FoldersCollection
        {
            get { return foldersCollection; }
            set
            {
                if (foldersCollection != value)
                {
                    foldersCollection = value;
                    OnPropertyChanged("FoldersCollection");
                }
            }
        }

        public LiveService.Folder SelectedFolder
        {
            get { return this.selectedfolder; }
            set
            {
               /* if (this.selectedfolder == value)
                {
                    this.mparent.RegisterNextView("Folder");
                }
                else
                {*/
                    this.selectedfolder = value;
                    OnPropertyChanged("SelectedFolder");
                    this.Test = "enculay";
               // }
            }
        }

        public String Test
        {
            get { return /*this.SelectedFolder.name.Length > 0 ? "455666":*/ "plip" ;}
            set { mtest = value;  OnPropertyChanged("Test"); }
        }
    }
}
